import emojiRegex from 'emoji-regex';  //npm install emoji-regex
import Vue from "vue";
// 检测是否有权限
// 使用Vue.directive声明自定义指令btn-key
export const buttonPermissions = Vue.directive("btn-key", {
  // 指令生命周期
  inserted(el, binding, vnode) {
    const { value } = binding;
    const all_permission = "*:*:*";
    // const permissions = JSON.parse(localStorage.getItem("btnlist"));
    const permissions = ['client:clue:update'];
    if (value && value instanceof Array && value.length > 0) {
      const permissionFlag = value;
      const hasPermissions = permissions.some(permission => {
        return (
          all_permission === permission || permissionFlag.includes(permission)
        );
      });
      if (!hasPermissions) {
        // el.style.display = 'none'
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      throw new Error(`请设置操作权限标签值`);
    }
  },
  updated(el, binding, vnode, prevVnode) {
    const { value } = binding;
    const all_permission = "*:*:*";
    // const permissions = JSON.parse(localStorage.getItem("btnlist"));
    const permissions = ['client:clue:update'];
    if (value && value instanceof Array && value.length > 0) {
      const permissionFlag = value;
      const hasPermissions = permissions.some(permission => {
        return (
          all_permission === permission || permissionFlag.includes(permission)
        );
      });
      if (!hasPermissions) {
        // el.style.display = 'none'
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      throw new Error(`请设置操作权限标签值`);
    }
  }
});

export const emoji = Vue.directive('clearEmoij', {
  bind(el, binding, vnode, oldVnode) {
    const regex = emojiRegex();
    const obj = el.querySelectorAll('.el-input__inner,.el-textarea__inner')[0];
    let zclearNoNum = function (e) {
      if (e.target.composing) return;
      let match = regex.exec(obj.value); //也可以直接用正则表达式判断
      if (match) {
        /*for (let i=0;i<match.length;i++) {
          obj.value = obj.value.replace(match[i],"");
        }*/
        obj.value = obj.value.replace(/(\ud83c[\udc00-\udfff])|(\ud83d[\udc00-\udfff])|(\ud83e[\udc00-\udfff])|[\u2100-\u32ff]|[\u0030-\u007f][\u20d0-\u20ff]|[\u0080-\u00ff]/g, "");
        zclearNoNum(e);
      }
      // 触发v-model的更新
      obj.dispatchEvent(new Event('input'));
    }
    let zblur = function (e) {
      zclearNoNum(e);
      setTimeout(() => {
        let match = regex.exec(obj.value);
        if (match) {
          obj.value = obj.value.replace(/(\ud83c[\udc00-\udfff])|(\ud83d[\udc00-\udfff])|(\ud83e[\udc00-\udfff])|[\u2100-\u32ff]|[\u0030-\u007f][\u20d0-\u20ff]|[\u0080-\u00ff]/g, "");
          zclearNoNum(e);
        }
        // 触发v-model的更新
        obj.dispatchEvent(new Event('change'));
      }, 50)
    }
    obj.onkeyup = zclearNoNum;
    obj.onblur = zblur;

    function onCompositionStart(e) {
      e.target.composing = true
    }
    function onCompositionEnd(e) {
      e.target.composing = false
      obj.dispatchEvent(new Event('input'));
    }
    // https://segmentfault.com/a/1190000009246058
    obj.addEventListener('compositionstart', onCompositionStart)
    obj.addEventListener('compositionend', onCompositionEnd)
  },
  update(el, binding, vnode, oldVnode) { }
})

export const domDrag = Vue.directive('domDrag', {
  bind(el) {
    //el即为当前元素，添加可拖拽标识
    el.style.cursor = 'move'
    // 获取原有属性 ie dom.currentStyle 火狐谷歌 window.getComputedStyle(dom, null);
    // el.onmousedown = (e) => {
    //   //获取鼠标按下位置
    //   const disX = e.clientX
    //   const disY = e.clientY
    //   // 获取当前元素的定位信息
    //   // 获取到的值带px 正则匹配替换
    //   let styL, styT
    //   // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
    //   // +的作用是将字符串转为数字
    //   if (sty.left.includes('%')) {
    //     styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
    //     styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
    //   } else {
    //     styL = +sty.left.replace(/\px/g, '')
    //     styT = +sty.top.replace(/\px/g, '')
    //   }
    //   document.onmousemove = function (e) {
    //     // 通过事件委托，计算移动的距离
    //     const l = e.clientX - disX
    //     const t = e.clientY - disY
    //     // 移动当前元素
    //     el.style.left = `${l + styL}px`
    //     el.style.top = `${t + styT}px`
    //   }
    //   //鼠标弹起，移除相应事件
    //   document.onmouseup = function (e) {
    //     document.onmousemove = null
    //     document.onmouseup = null
    //   }
    // }

    // 需要移动一定要添加宽高
    el.onmousedown = function (e) {
      //获取鼠标按下时候的位置
      var mx = e.clientX - this.offsetLeft;
      var my = e.clientY - this.offsetTop;
      //获取鼠标移动后的距离
      document.onmousemove = function (e) {
        var xlength = e.clientX - mx;
        var ylength = e.clientY - my;
        el.style.left = xlength + 'px';
        el.style.top = ylength + 'px';
      }
    }
    //鼠标抬起
    el.onmouseup = function () {
      //移除事件
      document.onmousemove = null;
    }
  }
})

